#include "welcome_dlg.h"
#include "ui_welcome_dlg.h"

#include "2-widgets/avatar_widget.h"
#include "0-data/threads/get_avatars_thread.h"
#include "0-data/threads/avatar_modify_thread.h"

#include <QScreen>

WelcomeDlg::WelcomeDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WelcomeDlg), dlg_(nullptr)
{
    ui->setupUi(this);

    QScreen *screen = QApplication::primaryScreen();
    this->resize(screen->geometry().size());

    InitData();
}

WelcomeDlg::~WelcomeDlg()
{
    delete ui;
}

void WelcomeDlg::InitData()
{
    GetAvatarsThread *thread = new GetAvatarsThread(this);
    connect(thread, SIGNAL(finished()), this, SLOT(slot_get_avatars_finished()));
    thread->start();
}

void WelcomeDlg::UpdateAvatarWidget()
{
    selected_avatar_list_.clear();
    for (int cc = ui->avatar_layout->count()-1; cc >= 0; --cc)
    {
        QLayoutItem *it = ui->avatar_layout->itemAt(cc);
        QWidget *old_widget = it->widget();
        if (old_widget != 0)
        {
            ui->avatar_layout->removeWidget(old_widget);
            old_widget->deleteLater();
        }
    }

    for (auto item : avatar_list_)
    {
        AvatarWidget *avatar_widget = new AvatarWidget(item.uuid(), item.name(), this);
        connect(avatar_widget, SIGNAL(signal_btn_checked(QString,bool)), this, SLOT(slot_avatars_clicked(QString,bool)));
        ui->avatar_layout->addWidget(avatar_widget);
    }

    AvatarWidget *insert_new_widget = new AvatarWidget("", true, this);
    connect(insert_new_widget, SIGNAL(signal_btn_checked(QString,bool)), this, SLOT(slot_avatars_clicked(QString,bool)));
    ui->avatar_layout->addWidget(insert_new_widget);

    ui->btn_confirm->setEnabled(selected_avatar_list_.size() > 0);
    ui->btn_delete->setEnabled(selected_avatar_list_.size() > 0);
}

void WelcomeDlg::slot_get_avatars_finished()
{
    GetAvatarsThread *thread = qobject_cast<GetAvatarsThread *>(sender());
    if(thread != nullptr)
    {
        thread->GetResult(avatar_list_);
        thread->deleteLater();

        UpdateAvatarWidget();
    }
}

void WelcomeDlg::slot_save_finished()
{
    AvatarModifyThread *thread = qobject_cast<AvatarModifyThread *>(sender());
    if(thread != nullptr)
    {
        thread->deleteLater();

        InitData();
    }
}

void WelcomeDlg::slot_avatars_clicked(QString uuid, bool checked)
{
    if(uuid == "INSERT")
    {
        if(dlg_ == nullptr)
        {
            dlg_ = new CreateNewAvatarDlg(this);
            connect(dlg_, SIGNAL(sig_accept()), this, SLOT(slot_insert_accepted()), Qt::UniqueConnection);
            connect(dlg_, SIGNAL(sig_reject()), this, SLOT(slot_insert_rejected()), Qt::UniqueConnection);
            dlg_->show();
        }
    }
    else if(checked)
    {
        selected_avatar_list_.append(uuid);
    }
    else
    {
        selected_avatar_list_.removeOne(uuid);
    }

    ui->btn_confirm->setEnabled(selected_avatar_list_.size() > 0);
    ui->btn_delete->setEnabled(selected_avatar_list_.size() > 0);
}

void WelcomeDlg::slot_insert_accepted()
{
    if(dlg_ == nullptr)
    {
        return;
    }

    QString avatar_name = dlg_->GetAvatarName();
    dlg_->close();

    delete dlg_;
    dlg_ = nullptr;

    QList<Avatar> list_avatar;
    Avatar avatar(avatar_name);
    list_avatar << avatar;

    AvatarModifyThread *thread = new AvatarModifyThread(this);
    thread->SetParams(list_avatar, AvatarModifyThread::ENUM_INSERT);
    connect(thread, SIGNAL(finished()), this, SLOT(slot_save_finished()));
    thread->start();
}

void WelcomeDlg::slot_insert_rejected()
{
    if(dlg_ == nullptr)
    {
        return;
    }

    dlg_->close();

    delete dlg_;
    dlg_ = nullptr;
}

void WelcomeDlg::on_btn_delete_clicked()
{
    QList<Avatar> list_avatar;
    for(auto item : selected_avatar_list_)
    {
        list_avatar << Avatar(item, "", 0);
    }

    AvatarModifyThread *thread = new AvatarModifyThread(this);
    thread->SetParams(list_avatar, AvatarModifyThread::ENUM_DELETE);
    connect(thread, SIGNAL(finished()), this, SLOT(slot_save_finished()));
    thread->start();
}

void WelcomeDlg::on_btn_confirm_clicked()
{

}

